import {
  _decorator,
  Component,
  Node,
  Sprite,
  SpriteFrame,
  UITransform,
} from "cc";
import { Constants } from "./Constants";
import { gameGlobalStatus } from "./GameGlobalStatus";
import { nodePoolManager } from "./Manager/NodePoolManager";
const { ccclass, property } = _decorator;

@ccclass("EnemyPlaneScripts")
export class EnemyPlaneScripts extends Component {
  @property([SpriteFrame])
  enemies: SpriteFrame[] = [];

  /// 飞机移动的速度，不同的飞机速度不同
  _planeSpeed: number = 1;

  start() {}

  /// 初始化
  init(
    enemyType: number = Constants.EnemyPlaneType.ENEMY1,
    planeSpeed: number = 1
  ) {
    this.node.getComponent(Sprite).spriteFrame = this.enemies[enemyType];
    this.node.name = "enemy_" + enemyType + "_" + planeSpeed;
    this._planeSpeed = planeSpeed * 20;
  }

  update(deltaTime: number) {
    const enemyPos = this.node.position;
    const moveLength = enemyPos.y - this._planeSpeed * deltaTime;
    this.node.setPosition(enemyPos.x, moveLength);

    if (
      moveLength <
      -(
        gameGlobalStatus.uiScreenHeight / 2 +
        this.node.getComponent(UITransform).height
      )
    ) {
      nodePoolManager.putNode(this.node);
    }
  }
}
